<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>最佳字段查询调优 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/_tuning_best_fields_queries.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/_tuning_best_fields_queries.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/_tuning_best_fields_queries.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/_tuning_best_fields_queries.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="search-in-depth.html">深入搜索</a></span>
»
<span class="breadcrumb-link"><a href="multi-field-search.html">多字段搜索</a></span>
»
<span class="breadcrumb-node">最佳字段查询调优</span>
</div>
<div class="navheader">
<span class="prev">
<a href="_best_fields.html">« 最佳字段</a>
</span>
<span class="next">
<a href="multi-match-query.html">multi_match 查询 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="_tuning_best_fields_queries"></a>最佳字段查询调优<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/110_Multi_Field_Search/20_Tuning_best_field_queries.asciidoc">edit</a>
</h2>
</div></div></div>
<p>当用户搜索 “quick pets” 时会发生什么呢？在前面的例子中，两个文档都包含词 <code class="literal">quick</code> ，但是只有文档 2 包含词 <code class="literal">pets</code> ，两个文档中都不具有同时包含 <em>两个词</em> 的 <em>相同字段</em> 。</p>
<p>如下，一个简单的 <code class="literal">dis_max</code> 查询会采用单个最佳匹配字段，而忽略其他的匹配：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">{
    "query": {
        "dis_max": {
            "queries": [
                { "match": { "title": "Quick pets" }},
                { "match": { "body":  "Quick pets" }}
            ]
        }
    }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/110_Multi_Field_Search/15_Best_fields.json"></div>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "hits": [
     {
        "_id": "1",
        "_score": 0.12713557, <a id="CO67-1"></a><i class="conum" data-value="1"></i>
        "_source": {
           "title": "Quick brown rabbits",
           "body": "Brown rabbits are commonly seen."
        }
     },
     {
        "_id": "2",
        "_score": 0.12713557, <a id="CO67-2"></a><i class="conum" data-value="1"></i>
        "_source": {
           "title": "Keeping pets healthy",
           "body": "My quick brown fox eats rabbits on a regular basis."
        }
     }
   ]
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO67-1"><i class="conum" data-value="1"></i></a><a href="#CO67-2"></a></p>
</td>
<td align="left" valign="top">
<p>注意两个评分是完全相同的。</p>
</td>
</tr>
</table>
</div>
<p>我们可能期望同时匹配 <code class="literal">title</code> 和 <code class="literal">body</code> 字段的文档比只与一个字段匹配的文档的相关度更高，但事实并非如此，因为 <code class="literal">dis_max</code> 查询只会简单地使用 <em>单个</em> 最佳匹配语句的评分 <code class="literal">_score</code> 作为整体评分。</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_tie_breaker_参数"></a>tie_breaker 参数<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/110_Multi_Field_Search/20_Tuning_best_field_queries.asciidoc">edit</a>
</h3>
</div></div></div>
<p>可以通过指定 <code class="literal">tie_breaker</code> 这个参数将其他匹配语句的评分也考虑其中：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">{
    "query": {
        "dis_max": {
            "queries": [
                { "match": { "title": "Quick pets" }},
                { "match": { "body":  "Quick pets" }}
            ],
            "tie_breaker": 0.3
        }
    }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/110_Multi_Field_Search/15_Best_fields.json"></div>
<p>结果如下：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "hits": [
     {
        "_id": "2",
        "_score": 0.14757764, <a id="CO68-1"></a><i class="conum" data-value="1"></i>
        "_source": {
           "title": "Keeping pets healthy",
           "body": "My quick brown fox eats rabbits on a regular basis."
        }
     },
     {
        "_id": "1",
        "_score": 0.124275915, <a id="CO68-2"></a><i class="conum" data-value="1"></i>
        "_source": {
           "title": "Quick brown rabbits",
           "body": "Brown rabbits are commonly seen."
        }
     }
   ]
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO68-1"><i class="conum" data-value="1"></i></a><a href="#CO68-2"></a></p>
</td>
<td align="left" valign="top">
<p>文档 2 的相关度比文档 1 略高。</p>
</td>
</tr>
</table>
</div>
<p><code class="literal">tie_breaker</code> 参数提供了一种 <code class="literal">dis_max</code> 和 <code class="literal">bool</code> 之间的折中选择，它的评分方式如下：</p>
<div class="olist orderedlist">
<ol class="orderedlist">
<li class="listitem">
获得最佳匹配语句的评分 <code class="literal">_score</code> 。
</li>
<li class="listitem">
将其他匹配语句的评分结果与 <code class="literal">tie_breaker</code> 相乘。
</li>
<li class="listitem">
对以上评分求和并规范化。
</li>
</ol>
</div>
<p>有了 <code class="literal">tie_breaker</code> ，会考虑所有匹配语句，但最佳匹配语句依然占最终结果里的很大一部分。</p>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p><code class="literal">tie_breaker</code> 可以是 <code class="literal">0</code> 到 <code class="literal">1</code> 之间的浮点数，其中 <code class="literal">0</code> 代表使用 <code class="literal">dis_max</code> 最佳匹配语句的普通逻辑， <code class="literal">1</code> 表示所有匹配语句同等重要。最佳的精确值需要根据数据与查询调试得出，但是合理值应该与零接近（处于 <code class="literal">0.1 - 0.4</code> 之间），这样就不会颠覆 <code class="literal">dis_max</code> 最佳匹配性质的根本。</p>
</div>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="_best_fields.html">« 最佳字段</a>
</span>
<span class="next">
<a href="multi-match-query.html">multi_match 查询 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>